package com.qcq.es.ESUtil;

import com.alibaba.fastjson.JSON;
import com.qcq.es.entity.PageResult;
import com.qcq.es.pojo.vo.GoodsVO;
import org.apache.commons.lang3.StringUtils;
import org.apache.http.HttpHost;
import org.apache.lucene.search.TotalHits;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.text.Text;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHits;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder;
import org.elasticsearch.search.fetch.subphase.highlight.HighlightField;
import org.elasticsearch.search.sort.SortOrder;

import java.util.ArrayList;
import java.util.List;

import static com.alibaba.fastjson.JSON.parseObject;

/**
 * @author ：qcq
 * @date ：Created in 2020/11/25 16:33
 * @description：
 */
public class ESUtils {

    private  int  temp = 0;

    public PageResult findPage(QueryBuilder queryBuilder, Integer from, Integer size , String keyword,int conId,Integer priceSort) throws Exception {


        RestHighLevelClient client = new RestHighLevelClient(
                RestClient.builder(new HttpHost(
                                "192.168.200.150", 9200, "http"
                        )
                )
        );



        PageResult result = new PageResult();

        List<GoodsVO> goodsVOList = new ArrayList<>();



        SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();

        sourceBuilder.query(queryBuilder);

        sourceBuilder.from(from);
        sourceBuilder.size(size);

        switch (conId) {
            case 0://综合排序
                sourceBuilder.sort("saleNum", SortOrder.DESC);
                sourceBuilder.sort("createTime", SortOrder.DESC);
                sourceBuilder.sort("commentNum", SortOrder.DESC);
                sourceBuilder.sort("price", SortOrder.DESC);
                break;
            case 1://销量排序
                sourceBuilder.sort("saleNum", SortOrder.DESC);
                break;
            case 2://时间排序
                sourceBuilder.sort("createTime", SortOrder.DESC);
                break;
            case 3://评论排序
                sourceBuilder.sort("commentNum", SortOrder.DESC);
                break;
            case 4://价格排序
                priceSort = (priceSort + 1) % 2;
                if (priceSort == 0) {
                    sourceBuilder.sort("price", SortOrder.DESC);
                } else {
                    sourceBuilder.sort("price", SortOrder.ASC);
                }
                //priceSort++;
                /*if (priceSort == 100) {
                    priceSort = 0;
                }*/
                break;
            default:
                break;
        }


        // ***** 设置高亮三要素
        HighlightBuilder highlight = SearchSourceBuilder.highlight();
        highlight.field("name");  // 高亮显示域
        highlight.preTags("<font color='red'>"); // 高亮显示前缀
        highlight.postTags("</font>");  // 高亮显示后缀
        sourceBuilder.highlighter(highlight);

        SearchRequest request = new SearchRequest("sku");

        sourceBuilder.trackTotalHits(true); //显示真实条数

        request.source(sourceBuilder);
        SearchResponse response = client.search(request, RequestOptions.DEFAULT);
        SearchHits searchHits = response.getHits();
        TotalHits total = searchHits.getTotalHits();
        System.out.println("总数为：" + total.value);
        //查询到的总数
        result.setTotal(total.value);
        SearchHit[] hits = searchHits.getHits();

        for (SearchHit hit : hits) {
            String userJson = hit.getSourceAsString();
            GoodsVO goodsVO = parseObject(userJson, GoodsVO.class);
            goodsVOList.add(goodsVO);


            if (!StringUtils.isBlank(keyword)) {
                HighlightField highlightField = hit.getHighlightFields().get("name");
                Text[] fragments = highlightField.getFragments();
                String name = StringUtils.join(fragments);
                // 判断如果是可以获取到数据则更新到用户对象中
                if (StringUtils.isNotBlank(name)) {
                    goodsVO.setName(name);
                }
            }



            System.out.println(goodsVOList);
        }
        result.setRows(goodsVOList);
        return result;
    }

}
